package heap

// 数组对象的一些方法
// 返回数组对象底层的数组
func (self *Object) Bytes() []int8      { return self.data.([]int8) }
func (self *Object) Shorts() []int16    { return self.data.([]int16) }
func (self *Object) Ints() []int32      { return self.data.([]int32) }
func (self *Object) Longs() []int64     { return self.data.([]int64) }
func (self *Object) Chars() []uint16    { return self.data.([]uint16) }
func (self *Object) Floats() []float32  { return self.data.([]float32) }
func (self *Object) Doubles() []float64 { return self.data.([]float64) }
func (self *Object) Refs() []*Object    { return self.data.([]*Object) }

// 返回数组的长度
func (self *Object) ArrayLength() int32 {
	switch self.data.(type) {
	case []int8:
		return int32(len(self.data.([]int8)))
	case []int16:
		return int32(len(self.data.([]int16)))
	case []int32:
		return int32(len(self.data.([]int32)))
	case []int64:
		return int32(len(self.data.([]int64)))
	case []uint16:
		return int32(len(self.data.([]uint16)))
	case []float32:
		return int32(len(self.data.([]float32)))
	case []float64:
		return int32(len(self.data.([]float64)))
	case []*Object:
		return int32(len(self.data.([]*Object)))
	default:
		panic("Not Array")
	}
}

// 新建一个具有指定容量的数组对象
func (self *Class) NewArray(count uint) *Object {
	// 不是数组类，不允许使用这个方法
	if !self.IsArray() {
		panic("Not Array Class")
	}
	
	switch self.name {
	case "[B", "[Z":
		// JVM中，boolean类型的数组，是用byte数组来表示的。byte的1代表true，byte的0代表false
		return &Object{class: self, data: make([]int8, count)}
	case "[C":
		return &Object{class: self, data: make([]uint16, count)}
	case "[S":
		return &Object{class: self, data: make([]int16, count)}
	case "[I":
		return &Object{class: self, data: make([]int32, count)}
	case "[J":
		return &Object{class: self, data: make([]int64, count)}
	case "[F":
		return &Object{class: self, data: make([]float32, count)}
	case "[D":
		return &Object{class: self, data: make([]float64, count)}
	default:
		return &Object{class: self, data: make([]*Object, count)}

	}
}
